//
//  DeliveyBoxTableViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/25.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "DeliveryBoxTableViewController.h"
#import "DeliveredResumeTableViewCell.h"
#import "DeliveredJobListTableViewCell.h"
#import "JobDetailViewController.h"
#import "PreviewResumeViewController.h"
#import <PGDatePickManager.h>

@interface DeliveryBoxTableViewController ()<UITableViewDelegate, UITableViewDataSource,DeliveredJobListCellDelegate1,DeliveredResumeCellDelegate1, PGDatePickerDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *jobArr;
@property (strong, nonatomic) NSMutableArray *resumeArr;


@end

@implementation DeliveryBoxTableViewController

- (void)viewWillAppear:(BOOL)animated
{
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 2 || role == 4) {
		[self getJobInfo];
	}
	else if(role == 5 || role == 6){
		[self getResumeInfo];
	}
}

- (void)viewDidLoad {
	[super viewDidLoad];
	
	// Do any additional setup after loading the view.
	[self createUI];
	[self refreshData];
}

- (void)createUI {
	
	self.view.backgroundColor = UISeparatedColorRGB;
	_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-120) style:UITableViewStylePlain];
	_tableView.delegate = self;
	_tableView.dataSource = self;
	[_tableView registerNib:[UINib nibWithNibName:@"DeliveredResumeTableViewCell" bundle:nil] forCellReuseIdentifier:@"resumeCell"];
	[_tableView registerNib:[UINib nibWithNibName:@"DeliveredJobListTableViewCell" bundle:nil] forCellReuseIdentifier:@"jobListCell"];
	_tableView.backgroundColor = UISeparatedColorRGB;
	_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.view addSubview:_tableView];
	
}

- (void)refreshData {
	__weak typeof (self) weakSelf = self;
	
	_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
		NSLog(@"下拉刷新");
		NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
		if (role == 2 || role == 4) {
			[weakSelf getJobInfo];
		}
		else if(role == 5 || role == 6){
			[weakSelf getResumeInfo];
		}
	}];
}

//TableView delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 2 || role == 4) {
		DeliveredJobListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jobListCell" forIndexPath:indexPath];
		cell.type = self.type;
		cell.info = self.jobArr[indexPath.row];
		cell.delegate1 = self;
		return cell;
		
	}
	else if(role == 5 || role == 6){
		DeliveredResumeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"resumeCell" forIndexPath:indexPath];
		cell.type = 1-self.type;
		cell.info = self.resumeArr[indexPath.row];
		cell.delegate1 = self;
		return cell;
	}
	else {
		return nil;
	}
	
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 2 || role == 4) {
		JobDetailViewController *vc = [[JobDetailViewController alloc] init];
		vc.position_id = self.jobArr[indexPath.row][@"position_id"];
		vc.shouldHideDeliverBtn = YES;	//一开始名字没设计好，应该叫shouldShowBtn之类的
		[self.navigationController pushViewController:vc animated:YES];
	}
	else if(role == 5 || role == 6){
		PreviewResumeViewController *vc = [[PreviewResumeViewController alloc] init];
		vc.resumeId = self.resumeArr[indexPath.row][@"resume_id"];
		vc.shouldShowInviteBtn = NO;	//一开始名字没设计好，应该叫shouldShowBtn之类的
		[self.navigationController pushViewController:vc animated:YES];
	}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 2 || role == 4) {
		return self.jobArr.count;
	}
	else if(role == 5 || role == 6){
		return self.resumeArr.count;
	}
	else{
		return 0;
	}
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 2 || role == 4) {
		if(self.type == 0){
			return 130;
		}
		else {
			return 160;
		}
	}
	else {
		if(self.type == 0){
			return 120;
		}
		else {
			return 120;
		}
	}
	
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	
	NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (role == 2 || role == 4) {
		if(self.type == 0){
			return 130;
		}
		else {
			return 160;
		}
	}
	else {
		if(self.type == 0){
			return 120;
		}
		else {
			return 120;
		}
	}

}

- (NSMutableArray *)jobArr
{
	if (!_jobArr) {
		_jobArr = [[NSMutableArray alloc] init];
	}
	return _jobArr;
}

- (NSMutableArray *)resumeArr
{
	if (!_resumeArr) {
		_resumeArr = [[NSMutableArray alloc] init];
	}
	return _resumeArr;
}

- (void)rejectInvitation:(NSString *)deliveryId
{
	UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定要拒绝吗？" message:nil preferredStyle:UIAlertControllerStyleAlert];
	[alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
	}]];
	[alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		[[NetworkTool sharedTool] requestWithName:@"拒绝对方邀约/申请" URLString:APICancelDelivery parameters:@{@"delivery_id":deliveryId} method:@"POST" callBack:^(id responseObject) {
			NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
			if (role == 2 || role == 4) {
				[self getJobInfo];
			}
			else if(role == 5 || role == 6){
				[self getResumeInfo];
			}
		}];
	}]];
	[self presentViewController:alert animated:YES completion:nil];
	
}

- (void)agreeInvitation:(NSString *)deliveryId
{
	UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定同意该次邀请？同意后请点击个人中心->面试管理(待面试)进行时间协商" message:nil preferredStyle:UIAlertControllerStyleAlert];
	[alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
	}]];
	[alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		[[NetworkTool sharedTool] requestWithName:@"同意面试" URLString:APIConfirmInvitation parameters:@{@"delivery_id":deliveryId} method:@"POST" callBack:^(id responseObject) {
			NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
			if (role == 2 || role == 4) {
				[self getJobInfo];
			}
			else if(role == 5 || role == 6){
				[self getResumeInfo];
			}
		}];
	}]];
	[self presentViewController:alert animated:YES completion:nil];
}

- (void)chooseDateForDeliveryId:(NSString *)deliveryId
{
	PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
	PGDatePicker *datePicker = datePickManager.datePicker;
	datePicker.datePickerMode = PGDatePickerModeDateHourMinute;
	datePicker.delegate = self;
	[self presentViewController:datePickManager animated:false completion:nil];
	datePicker.selectedDate = ^(NSDateComponents *dateComponents) {
		NSLog(@"dateComponents = %@", dateComponents);
		NSCalendar *calender = [NSCalendar currentCalendar];
		NSDate *date = [calender dateFromComponents:dateComponents];
		NSTimeInterval interval = [date timeIntervalSince1970];
		NSLog(@"时间戳%d",(int)interval);
		NSDictionary *param = @{@"delivery_id":deliveryId,
								@"time":[NSString stringWithFormat:@"%d", (int)interval]
								};
		[[NetworkTool sharedTool] requestWithName:@"企业选择时间" URLString:APICompanyChooseDate parameters:param method:@"POST" callBack:^(id responseObject) {
			NSInteger role = [GetUserDefaule(USERINFO)[@"role"] integerValue];
			if (role == 2 || role == 4) {
				[self getJobInfo];
			}
			else if(role == 5 || role == 6){
				[self getResumeInfo];
			}
		}];
	};
}

/*
#pragma PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
	NSLog(@"dateComponents = %@", dateComponents);
}
*/

- (void)getJobInfo
{
	[[NetworkTool sharedTool] requestWithName:@"获取个人投递箱信息" URLString:APIDeliveryBoxPerson parameters:@{@"type":[NSString stringWithFormat:@"%ld",self.type+1]} method:@"POST" callBack:^(id responseObject) {
		
		[self.jobArr removeAllObjects];
		NSArray *arr = responseObject[@"data"];
		for (NSDictionary *dict in arr){
			[self.jobArr addObject:dict];
		}
		dispatch_async(dispatch_get_main_queue(), ^{
			[MBProgressHUD hideHUDForView:self.view animated:YES];
			[self.tableView.mj_header endRefreshing];
			[self.tableView reloadData];
		});
	}];
}

- (void)getResumeInfo
{
	[[NetworkTool sharedTool] requestWithName:@"获取企业投递箱信息" URLString:APIDeliveryBoxCompany parameters:@{@"type":[NSString stringWithFormat:@"%ld",self.type+1]} method:@"POST" callBack:^(id responseObject) {
		
		[self.resumeArr removeAllObjects];
		NSArray *arr = responseObject[@"data"];
		for (NSDictionary *dict in arr){
			[self.resumeArr addObject:dict];
		}
		dispatch_async(dispatch_get_main_queue(), ^{
			[MBProgressHUD hideHUDForView:self.view animated:YES];
			[self.tableView.mj_header endRefreshing];
			[self.tableView reloadData];
		});
	}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
